package com.doria.sso.controller;

import com.doria.common.ad_enum.CodeEnum;
import com.doria.common.ad_enum.MsgEnum;
import com.doria.sso.dto.LoginDto;
import com.doria.common.pojo.vo.HttpResult;
import com.doria.sso.dto.SendLoginCodeDto;
import com.doria.sso.service.interfaces.IAuthService;
import com.doria.sso.service.interfaces.ISmsService;
import com.doria.sso.utils.JwtUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 安全相关
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final JwtUtils jwtUtils;
    private final HttpServletResponse response;
    private final IAuthService authService;
    private final ISmsService smsService;

    public AuthController(JwtUtils jwtUtils, HttpServletResponse response, IAuthService authService, ISmsService smsService) {
        this.jwtUtils = jwtUtils;
        this.response = response;
        this.authService = authService;
        this.smsService = smsService;
    }

    @PostMapping("/sendLoginCode")
    public HttpResult sendLoginCode(SendLoginCodeDto sendLoginCodeDto){
        return smsService.sendSmsCode(sendLoginCodeDto.getMobile());
    }

    @PostMapping("/login")
    public HttpResult login(@RequestBody LoginDto loginDto){
       return authService.login(loginDto);
    }
    @PostMapping("/checkToken")
    public HttpResult checkToken(@RequestBody String token){
        if (token==null||token.trim().equals("")){
            response.setStatus(400);
            return new HttpResult(false, CodeEnum.BADE_PARAM, MsgEnum.BAD_PARAM);
        }
        return jwtUtils.checkToken(token);
    }


}
